﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TeamTime.Classes;
using StillVoiceCommon;

namespace TeamTime
{
    public partial class TN5YProjectFormControl : System.Web.UI.UserControl
    {
        public string SaveButtonText { get { return this.btnSave.Text; } set { this.btnSave.Text = value; } }

        public bool SaveButtonVisible { get; set; }
        //private bool liteMode;


        public bool SuccessfullySaved
        {
            get { return this.chkSuccessfullySaved.Checked; }
            set { this.chkSuccessfullySaved.Checked = value; }
        }
        public Int32 CompanyID
        {
            get
            {
                if (this.txtCompanyID.Text == "") this.txtCompanyID.Text = "0";
                return Convert.ToInt32(this.txtCompanyID.Text);
            }
            set
            {
                this.txtCompanyID.Text = value.ToString();
            }
        }
        public Int32 ProjectID
        {
            get { return Convert.ToInt32(this.txtID.Text); }
            set
            {
                //this.txtID.Text = .ToString();
                LoadDetailsForm(value);
            }
        }
        

        public event System.EventHandler SaveClicked;
        private void TN5YProjectFormControl_SaveClicked(object sender, EventArgs e) { }
        public delegate void SaveClickEventHandler(object sender, EventArgs e);

        public event System.EventHandler LoadDupClicked;
        private void TN5YProjectFormControl_LoadDupClicked(object sender, EventArgs e) { }
        public delegate void LoadDupClickEventHandler(object sender, EventArgs e);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
               // Functions.dropDownListPopulate(this.ddlContactTypeID, sqlDB.getDataSet("SELECT ID,Description FROM ContactType order by Description"), "ID", "Description");

                EnableControls();
            }
        }
        public void EnableControls()
        {
            Functions.enableChildControls(this.pnlDetails);
            this.txtUserNameModifiedBy.Enabled = true;
            this.txtRecordDate.Enabled = true;
            this.trID.Visible = (txtID.Text != "");
            this.txtID.Enabled = false;
        }
        public void setForAdding()
        {
            Functions.clearChildcontrols(this.pnlDetails);
            
        }
        private void LoadDetailsForm(Int32 ID)
        {
            TN5YProject tN5YProject = new TN5YProject();
            tN5YProject.load(ID.ToString());
            //this.reqValPersonName.Enabled = false;
            Functions.populateFormControlsFromDataTableFields(tN5YProject, this.pnlDetails);
            this.txtCompanyName.Text = tN5YProject.CompanyName;
        }
        public void btnSave_Click(object sender, EventArgs e)
        {
            SuccessfullySaved = false;
            TN5YProjects tN5YProjects = new TN5YProjects();

            TN5YProject tN5YProject = new TN5YProject();

            if (this.txtID.Text == "") { tN5YProject.setForAdd(); } else { tN5YProject.load(this.txtID.Text); }

            
                Companies companies = new Companies();
                int companyID = companies.GetCompanyIDFromName(this.txtCompanyName.Text);
                if (companyID == -1) //No company found
                {
                    this.reqValCompanyName.IsValid = false;
                    this.reqValCompanyName.ErrorMessage = "* Company does not exist";
                    return;
                }
                else
                    tN5YProject.Fields["CompanyID"] = companyID;

                tN5YProject.Fields["ProjectName"] = this.txtProjectName.Text;
                
                tN5YProject.Fields["Category"] = this.txtCategory.Text;
                tN5YProject.Fields["Priority"] = this.txtPriority.Text;
                tN5YProject.Fields["Status"] = this.txtStatus.Text;
                tN5YProject.Fields["Begin"] = this.calBegin.Text;
                if (this.calEnd.Text != "") { tN5YProject.Fields["End"] = this.calEnd.Text; } else { tN5YProject.Fields["End"] = DBNull.Value; }
                if (Functions.IsNumeric(this.txtBudget.Text)) tN5YProject.Fields["Budget"] = this.txtBudget.Text;
                if (Functions.IsNumeric(this.txtPONumber.Text)) tN5YProject.Fields["PONumber"] = this.txtPONumber.Text;
                tN5YProject.Fields["Notes"] = this.txtNotes.Text;
           
            tN5YProject.UserIDModifiedBy = Convert.ToInt32(Session["UserID"]);
            tN5YProject.updateWithUserIDDate(); //Updates RecordDate
            this.ProjectID = Convert.ToInt32(tN5YProject.KeyValue);
            //if (this.CompanyID != 0)
            //{
            //    tN5YProject.AddToCompany(CompanyID);
            //}
            tN5YProject = null;
            SuccessfullySaved = true;
            lblSaveMessage.ShowStatusMessage("Successfully saved");


            if (this.SaveClicked != null)
                this.SaveClicked(sender, e);

        }
        //protected void lnkLoadDup_Click(object sender, EventArgs e)
        //{
        //    if (this.lnkLoadDup.Attributes["DupID"] != "")
        //    {
        //        LoadDetailsForm(Convert.ToInt32(this.lnkLoadDup.Attributes["DupID"]));
        //        this.lnkLoadDup.Visible = false;
        //    }

        //    if (this.LoadDupClicked != null)
        //        this.LoadDupClicked(sender, e);
        //}

        //private void Save()
        //{


        //}
       
    }
}